<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Advertise_Model extends MY_Model {
    public function __construct() {
        parent::__construct();
        $this->load->model('entity/advertise_entity', 'Advertise_Entity');
    }

    public function getById($id){
        $this->db->select('id,user_id,opt_info,order,start_date,end_date,position, anchor, content,publish_flg,deleted_at', true);
        $this->db->from($this->table_advertise_name);
        $this->db->where('id', $id);
        $result = $this->db->get()->first_row('Advertise_Entity');
        return $result;
    }

    public function getAdvertise()
    {
        $this->db->select('id, position, anchor, content', true);
        $this->db->from($this->table_advertise_name);
        $this->db->where(array('deleted_at' => null, 'publish_flg' => 1));
        $this->db->order_by('position', 'ASC');
        $this->db->order_by('order', 'ASC');
        $result = $this->db->get()->result('Advertise_Entity');
        $ad[ADVERTISE_POS_1] = $ad[ADVERTISE_POS_2] = array();
        foreach ($result as $a) {
            array_push($ad['position_' . $a->getPosition()], $a);
        }
        return $ad;
    }

    /**
     * For Admin only
     * Get all advertise
     */
    public function getAllAdvertise()
    {
        $this->db->select('a.id, a.user_id, a.position, a.order, a.anchor, a.start_date, a.end_date, a.content, a.publish_flg, a.deleted_at, b.full_name', true);
        $this->db->from($this->table_advertise_name . ' AS a');
        $this->db->join($this->table_user_name . ' b', 'a.user_id=b.id');
        $this->db->where('b.deleted_at', NULL);
        $this->db->where('b.active_flg', 1);
        $result1 = $this->db->get()->result('Advertise_Entity');

        $this->db->select('id, opt_info, position, order, anchor, start_date, end_date, content, publish_flg, deleted_at', true);
        $this->db->from($this->table_advertise_name . ' AS a');
        $this->db->where('a.user_id', NULL);
        $result2 = $this->db->get()->result('Advertise_Entity');
        $result = array_merge($result1, $result2);

        return $result;
    }

    /**
     * For admin only
     * Add new advertise
     */
    public function insert($advertise)
    {
        $this->db->trans_begin();
        $this->db->insert($this->table_advertise_name, $advertise);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $id = $this->db->insert_id();
            $this->db->trans_commit();
            return $id;
        }
        return false;
    }

    /**
     * For admin only
     * Add new advertise
     */
    public function clientRegister($advertise)
    {
        $this->db->trans_begin();
        $this->db->insert($this->table_advertise_temp_name, $advertise);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * update a advertise
     */
    public function update($advertise_id, $advertise)
    {
        $this->db->trans_begin();
        $this->db->where('id', $advertise_id);
        $this->db->update($this->table_advertise_name, $advertise);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    public function getUserAdvertise($user_id){
        $this->db->select('id, position, anchor, content', true);
        $this->db->from($this->table_advertise_name);
        $this->db->where(array(
                'deleted_at' => null,
                'publish_flg' => 1,
                'user_id' => $user_id
            )
        );
        $this->db->order_by('position', 'ASC');
        $this->db->order_by('order', 'ASC');
        $result = $this->db->get()->result('Advertise_Entity');
        $ad[ADVERTISE_POS_1] = $ad[ADVERTISE_POS_2] = $ad[ADVERTISE_POS_3] = array();
        foreach ($result as $a) {
            array_push($ad['position_' . $a->getPosition()], $a);
        }
        return $ad;
    }

    public function getRegisterAdvertise(){
        $this->load->model('entity/advertise_temp_entity', 'Advertise_Temp_Entity');

        $this->db->select('a.id, a.user_id, a.position, a.anchor, a.start_date, a.end_date, a.file_name, a.note, b.full_name, a.created_at, a.deleted_at', true);
        $this->db->from($this->table_advertise_temp_name . ' AS a');
        $this->db->join($this->table_user_name . ' b', 'a.user_id=b.id');
        $this->db->where('b.deleted_at', NULL);
        $this->db->where('b.active_flg', 1);
        $result1 = $this->db->get()->result('Advertise_Temp_Entity');

        $this->db->select('id, opt_info, position, anchor, start_date, end_date, file_name, note, created_at, deleted_at', true);
        $this->db->from($this->table_advertise_temp_name . ' AS a');
        $this->db->where('a.user_id', NULL);
        $this->db->order_by('a.id', 'DESC');
        $result2 = $this->db->get()->result('Advertise_Temp_Entity');
        $result = array_merge($result1, $result2);

        return $result;
    }

    public function updateTempRegister($advertise_id, $advertise)
    {
        $this->db->trans_begin();
        $this->db->where('id', $advertise_id);
        $this->db->update($this->table_advertise_temp_name, $advertise);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }
}
?>